package com.example.mymvvmdemo.utils;

import android.content.Context;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

public class TextColorUtils {

    public interface TextColorListener {
        void onLeftClick();

        void onRightClick();
    }

    public static void setTextStyle(TextView textView, String text, int selectColor) {
        int commaIndex = text.indexOf(",");
        if (commaIndex != -1) {
            SpannableString spannableString = new SpannableString(text);
            spannableString.setSpan(new ForegroundColorSpan(selectColor), 0, commaIndex + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(spannableString);
        }
    }

    /**
     * 更改特殊颜色文本
     */
    public static void initText(Context context, TextView textView, String all, String before, String after,
                                int selectColor, int backColor, TextColorListener listener) {
        String str = all;
        String terms = before;
        String privacy = after;
        SpannableStringBuilder ssb = new SpannableStringBuilder();
        ssb.append(str);
        final int start = str.indexOf(terms); //第一个出现的位置
        ssb.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                if (listener != null) {
                    listener.onLeftClick();
                }
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setFakeBoldText(true);
                ds.setColor(selectColor); //设置文件颜色
                ds.setUnderlineText(false); //去掉下划线
            }
        }, start, start + terms.length(), 0);
        final int end = str.lastIndexOf(privacy); //最后一个出现的位置
        ssb.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                if (listener != null) {
                    listener.onRightClick();
                }
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setFakeBoldText(true);
                ds.setColor(selectColor); //设置文子颜色
                ds.setUnderlineText(false); //去掉下划线
            }
        }, end, end + privacy.length(), 0);
        textView.setMovementMethod(LinkMovementMethod.getInstance()); //加上这句话才有效果
        textView.setText(ssb, TextView.BufferType.SPANNABLE); //加上这句话才有效果
        textView.setHighlightColor(ContextCompat.getColor(context, backColor)); //去掉点击后的背景颜色为透明
    }

    public static String getStr(String all, String str) {
        int i = all.indexOf(str);
        String substring = all.substring(0, i);
        return substring;
    }

}
